#include <iostream>
#include <fstream>
#include <vector>

#include "lexer.hpp"
#include "token.hpp"
#include "parser.hpp"
#include "generator.hpp"

std::string compile(const std::string& code);

int main(int argc, char** argv) {
    if (argc != 2) {
        std::cerr << "Write name of file!!!" << std::endl;
        return 1;
    }

    std::ifstream infile(argv[1]);

    if (!infile.is_open()) {
        std::cerr << "Can't open file!!!" << std::endl;
        return 1;
    }

    std::string input_code((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());

    infile.close();

    std::cout << compile(input_code);

    return 0;
}

std::string compile(const std::string& code) {
    std::vector<Token> tokens = tokenize(code);
    std::vector<Statement*> statements = parse(tokens);
    std::string output = generate(statements);

    for (auto& ptr : statements) delete ptr;

    return output;
}
